package book.nettyinaction.ch02;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class EchoClient {
    private final String host;
    private final int port;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public static void main(String[] args) throws InterruptedException {
        int port = 8080;
        String host = "localhost";

        if ( args.length > 1 ) {
            host = args[0];
            port = Integer.parseInt( args[1] );
        }

        new EchoClient(host, port).start();
    }

    private void start() throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            // 创建 Bootstrap
            Bootstrap b = new Bootstrap();
            b.group(group)  // 指定 EventLoopGroup 以处理客户端事件；需要适用于 NIO 的实现
                    .channel(NioSocketChannel.class)  // 适用于 NIO 传输的 Channel 类型
                    .remoteAddress(new InetSocketAddress(host, port))  // 设置服务器的 InetSocketAddress
                    .handler(new ChannelInitializer<SocketChannel>() {  // 在创建 Channel 时，向 ChannelPipeline 中添加一个 EchoClientHandler 实例
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new EchoClientHandler());
                        }
                    });

            // 连接到远程节点，阻塞等待直到连接完成
            ChannelFuture f = b.connect().sync();

            // 阻塞，直到 Channel 关闭
            f.channel().closeFuture().sync();
        } finally {

            // 关闭线程池并且释放所有的资源
         group.shutdownGracefully().sync();
        }
    }
}
